package com.jsmtr.rms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.DateTools;

import nc.webservice.hr.IHRExternalServiceStub;
import nc.webservice.hr.IHRExternalServiceStub.ReceivePsn;


@Controller
@RequestMapping("/IHR")
public class DoHRController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoResumeService doResumeService;

    /**
     * 将应聘通过的人员信息回传到HR系统
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/SendPerson", method = RequestMethod.POST)
    public void SendPerson(HttpServletRequest request, HttpServletResponse response) {
        try {
            //简历ID集合
            String[] uids = request.getParameterValues("uid");
            if (uids.length == 0) {
                utilService.printJsonResult(false, "请选择人员", "no", response);
                return;
            }
            //进入集团日期
            String indate = request.getParameter("indate");

            //员工来源
            String ufrom = request.getParameter("ufrom");
            String basgroupdef6 = "-";
            if (ufrom.equals("SZ")) {
               // basgroupdef6 = "从城镇招收";
                basgroupdef6 = "从社会招收";
            }
            if (ufrom.equals("XZ")) {
                basgroupdef6 = "录用应届高校大学毕业生";
            }
            if (ufrom.equals("ZZ")) {
                basgroupdef6 = "录用应届大中专技校毕业生";
            }

            for (String uid : uids) {
                DUserResume user = doResumeService.getDUserResume(uid);

                IHRExternalServiceStub stub = new IHRExternalServiceStub();
                ReceivePsn psn = new ReceivePsn();
                StringBuilder json = new StringBuilder();
                json.append("{\"bd_psnbasdoc\":{\"psnname\":\"" + user.getName() + "\",");
                json.append("\"id\":\"" + user.getCardno() + "\",");
                json.append("\"sex\":\"" + user.getSexcode() + "\",");
                json.append("\"nationality\":\"" + user.getMzcode() + "\",");
                json.append("\"birthdate\":\"" + DateTools.toDayStr(user.getBirthday()) + "\",");
                json.append("\"mobile\":\"" + user.getTel() + "\",");
                json.append("\"email\":\"" + user.getEmail() + "\",");
                json.append("\"basgroupdef27\":\"" + user.getNowaddress() + "\",");
                json.append("\"characterrpr\":\"" + user.getHkcode() + "\",");
                json.append("\"basgroupdef4\":\"" + user.getHukouaddress() + "\",");
                json.append("\"polity\":\"" + user.getZzmmcode() + "\",");
                json.append("\"basgroupdef1\":\"" + user.getHeight() + "\",");
                json.append("\"marital\":\"" + user.getMcode() + "\",");
                json.append("\"homephone\":\"" + user.getHometel() + "\",");

                //籍贯
                json.append("\"basgroupdef18\":\"" + user.getJg() + "\",");
                //员工来源
                json.append("\"basgroupdef6\":\"" + basgroupdef6 + "\",");
                json.append("\"health\":\"0101\",");
                json.append("\"basgroupdef5\":\"01\",");
                json.append("\"joinsysdate\":\"" + indate + "\"},");

                //学历信息
                json.append("\"hi_psndoc_edu\":[");
                List<DUserEducation> edus = doResumeService.getUserEducation(uid);
                for (DUserEducation edu : edus) {
                    json.append("{\"begindate\":\"" + DateTools.toDayStr(edu.getStarttime()) + "\",");
                    json.append("\"enddate\":\"" + DateTools.toDayStr(edu.getEndtime()) + "\",");
                    json.append("\"school\":\"" + edu.getSchoolname() + "\",");
                    json.append("\"groupdef3\":\"" + edu.getMajorname() + "\",");
                    json.append("\"education\":\"" + edu.getEducationcode() + "\",");
                    json.append("\"groupdef5\":\"" + edu.getTcode() + "\",");
                    json.append("\"degree\":\"" + edu.getDegreecode() + "\",");
                    json.append("\"groupdef2\":\"" + edu.getZscode() + "\"},");
                }
                if (edus.size() > 0) {
                    json.delete(json.length() - 1, json.length());
                }
                json.append("],");

                //工作经历
                json.append("\"hi_psndoc_work\":[");
                List<DUserWorkExperience> works = doResumeService.getUserWorkExperience(uid);
                for (DUserWorkExperience work : works) {
                    json.append("{\"workcorp\":\"" + work.getCompanyname() + "\",");
                    json.append("\"workpost\":\"" + work.getPosition() + "\",");
                    json.append("\"workdept\":\"" + work.getDepartment() + "\",");
                    json.append("\"begindate\":\"" + work.getStarttime() + "\",");
                    json.append("\"enddate\":\"" + work.getEndtime() + "\",");
                    json.append("\"groupdef1\":\"" + work.getJobduties() + "\",");
                    json.append("\"vdismismatter\":\"" + work.getReason() + "\"},");
                }
                if (works.size() > 0) {
                    json.delete(json.length() - 1, json.length());
                }
                json.append("],");

                //专业技术资格
                json.append("\"hi_psndoc_spetech\":[");
                if (user.getPosttypecode() != null
                        && user.getPostlevelcode() != null) {
                    json.append("{\"groupdef9\":\"" + user.getPosttypecode() + "\",");
                    json.append("\"groupdef1\":\"" + user.getPostspecialty() + "\",");
                    json.append("\"ctechname\":\"" + user.getPostlevelcode() + "\",");
                    json.append("\"dindate\":\"" + DateTools.toDayStr(user.getPostgettime()) + "\",");
                    json.append("\"cprounit\":\"" + user.getPostcompany() + "\"}");
                }
                json.append("],");

                //职业资格证
                json.append("\"hi_psndoc_nationduty\":[");
                if (user.getQllevelcode() != null) {
                    json.append("{\"groupdef4\":\"" + user.getQllevelpid() + "\",");
                    json.append("\"cworkname\":\"" + user.getQllevelcode() + "\",");
                    json.append("\"groupdef6\":\"" + user.getQllevelscode() + "\",");
                    json.append("\"groupdef3\":\"" + user.getQltype() + "\",");
                    json.append("\"dworkdate\":\"" + DateTools.toDayStr(user.getQlgettime()) + "\",");
                    json.append("\"cworkunitname\":\"" + user.getQlorg() + "\"}");
                }
                json.append("],");

                //其他职业资格证
                json.append("\"hi_psndoc_grpdef8\":[");
                if (user.getOthname() != null) {
                    json.append("{\"groupdef1\":\"" + user.getOthname() + "\",");
                    json.append("\"groupdef2\":\"" + user.getOthzy() + "\",");
                    json.append("\"groupdef3\":\"" + user.getOthlevel() + "\",");
                    json.append("\"groupdef4\":\"" + user.getOthcode() + "\",");
                    json.append("\"groupdef6\":\"" + DateTools.toDayStr(user.getOthdate()) + "\",");
                    json.append("\"groupdef7\":\"" + user.getOthorg() + "\"}");
                }
                json.append("],");

                //家庭成员
                json.append("\"hi_psndoc_family\":[");
                List<DUserFamily> familys = doResumeService.getUserFamily(uid);
                for (DUserFamily family : familys) {
                    json.append("{\"mem_name\":\"" + family.getName() + "\",");
                    json.append("\"mem_relation\":\"" + family.getRelationship() + "\",");
                    json.append("\"mem_corp\":\"" + family.getWorkunit() + "\",");
                    json.append("\"mem_job\":\"" + family.getProfession() + "\",");
                    json.append("\"vrelaphone\":\"" + family.getContactway() + "\",");
                    json.append("\"mem_birthday\":\"" + DateTools.toDayStr(family.getBirthday()) + "\"},");
                }
                if (familys.size() > 0) {
                    json.delete(json.length() - 1, json.length());
                }
                json.append("],");

                //通讯信息
                json.append("\"hi_psndoc_grpdef2\":[");
                json.append("{\"groupdef3\":\"" + user.getNowaddress() + "\",");
                json.append("\"groupdef7\":\"" + user.getCardaddress() + "\",");
                json.append("\"groupdef1\":\"" + user.getTel() + "\",");
                json.append("\"groupdef8\":\"Y\"}");
                json.append("],");

                //HR_工作信息
                json.append("\"bd_psndoc\":[{");
                json.append("\"psnname\":\"" + user.getName() + "\",");
                json.append("\"indocflag\":\"N\",");
                json.append("\"poststat\":\"Y\",");
                json.append("\"regular\":\"N\",");
                json.append("\"pk_psncl\":\"01\",");
                json.append("\"indutydate\":\"" + indate + "\",");
                json.append("\"onpostdate\":\"" + indate + "\",");

           /*      BaseJob bJob = null;
               if (ufrom.equals("SZ")) {
                    //社招
//                    bJob = templateService.getJobByppdid(user.getJid());
                } else {
                    //校招和中专
//                    bJob = templateService.getJobByCode(user.getJid());
                }*/

                ProjectInfoJobs projectInfoJobsTemp = doResumeService.getProjectJobs(user.getJid());
                BaseDepartment baseDepartmentTemp = doResumeService.getBaseDept(projectInfoJobsTemp.getJobdepart());
                BaseJob bJob = doResumeService.getBaseJob(baseDepartmentTemp.getCode(),projectInfoJobsTemp.getJobname());

                //所属部门
                json.append("\"pk_deptdoc\":\"" + bJob.getDeptcode() + "\",");
                //所在部门
                json.append("\"groupdef1\":\"" + bJob.getDeptcode() + "\",");
                //岗位
                json.append("\"pk_om_job\":\"" + bJob.getCode() + "\",");
                //岗位序列
                json.append("\"jobseries\":\"" + bJob.getJobseries() + "\",");
                //岗位等级
                json.append("\"jobrank\":\"" + bJob.getJobrank() + "\"}");
                json.append("]}");
                System.out.println(json);
                psn.setString(json.toString());
                System.out.println(stub.receivePsn(psn).get_return());
            }
            utilService.printJsonResult(true, "操作成功！", "no", response);

        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }

    }


}
